/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Utils;

import java.io.Serializable;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

/**
 *
 * @author aleX
 */
public class dbManager implements Serializable{
    //transient==non viene serializzato
    private transient Connection con;
   
    //String dburl = "jdbc:derby://localhost:1527/dbmercato";
    //jdbc:derby:C:/Users/Alice/Documents/NetBeansProjects/webintro-project/dbmercato
    public dbManager(String dburl)throws SQLException{
     try{
         Class.forName("org.apache.derby.jdbc.EmbeddedDriver", true,
                 getClass().getClassLoader());
         } catch(Exception e) {
            throw new RuntimeException(e.toString(), e);
         }
         Connection con = DriverManager.getConnection(dburl);
         this.con = con;
      }
  

    public static void shutdown(){
        try{
            DriverManager.getConnection("jdbc:derby://localhost:1527/dbmercato;shutdown=true");
        }catch(SQLException ex){
            Logger.getLogger(dbManager.class.getName()).info(ex.getMessage());
        }
    }
    //AUTENTICA UTENTE
public User authenticate(String username, String password) throws SQLException{
    PreparedStatement stm= con.prepareStatement("SELECT * "
            + "FROM User"
            + "WHERE username=? AND password=?");

    try{
        stm.setString(1, username);
        stm.setString(2, password);
        ResultSet rs= stm.executeQuery();
    try{
        if (rs.next()){
        User user= new User();
        user.setUsername(username);
        return user;
        }else{
          return null;
        }
    }finally{
    rs.close();
    }
    }finally{
    stm.close();
}}


public List<Agrumi> getProdotto() throws SQLException{
    List<Agrumi> agrumi= new ArrayList<Agrumi>();
    PreparedStatement stm=con.prepareStatement("SELECT *"
            + "FROM Agrumi");
    try{
        ResultSet rs= stm.executeQuery();
        try{
            while(rs.next()){
                Agrumi a= new Agrumi();
                a.setProdotto(rs.getString("prodotto"));
                a.setPrice(rs.getDouble("price"));
                a.setQuantity(rs.getInt("quantity"));
                a.setSeller(rs.getString("seller"));
                a.setUm(rs.getString("um"));
                
                agrumi.add(a);
            }} finally{
            rs.close();
        } } finally {
        stm.close();
    } return agrumi;
    }
}






    /**public static Connection getConnection() {
    

         Connection con = null;
         String url = "jdbc:derby://localhost:1527/dbmercato";
         
        try {

            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection(url, "swengdevel",  ".swengdevel.");            

        } catch (Exception ex) {
        ex.printStackTrace();
        }
        
        return con;          

    }
 
}
*/
